﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，模块
    /// </summary>
    public class ModuleDAL : DalBase, IModule
    {
        public ModuleDAL() 
        {
            Schema = "am.";
            TableName = "Module";
            ViewTableName = "V_Module";
            Fields = "ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName";
        }
        #region IModule 成员
        /// <summary>
        /// 新增模块
        /// </summary>
        /// <param name="info"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int InsertModule(ModuleInfo info, out string message)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleTypeID",SqlDbType.Int)
                ,new SqlParameter("@ModuleName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@ModuleLink",SqlDbType.VarChar,100)
                ,new SqlParameter("@ModuleMenu",SqlDbType.Char,1)
                ,new SqlParameter("@RtnID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
                ,new SqlParameter("@RtnMsg",SqlDbType.NVarChar,100)
            };
            int i = 0;
            parm[i++].Value = info.ModuleType.ModuleTypeID;
            parm[i++].Value = info.ModuleName;
            parm[i++].Value = info.ModuleLink;
            parm[i++].Value = info.ModuleMenu;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddModule", parm);
            message = parm[parm.Length - 1].Value.ToString();
            if (parm[parm.Length - 2].Value.ToString().Equals("80"))
                return int.Parse(parm[parm.Length - 3].Value.ToString());
            return 0;
        }

        /// <summary>
        /// 修改模块
        /// </summary>
        /// <param name="info"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdateModule(ModuleInfo info, out string message)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleID",SqlDbType.Int)
                ,new SqlParameter("@ModuleTypeID",SqlDbType.Int)
                ,new SqlParameter("@ModuleName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@ModuleLink",SqlDbType.VarChar,100)
                ,new SqlParameter("@ModuleMenu",SqlDbType.Char,1)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
                ,new SqlParameter("@RtnMsg",SqlDbType.NVarChar,100)
            };
            int i = 0;
            parm[i++].Value = info.ModuleID;
            parm[i++].Value = info.ModuleType.ModuleTypeID;
            parm[i++].Value = info.ModuleName;
            parm[i++].Value = info.ModuleLink;
            parm[i++].Value = info.ModuleMenu;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateModule", parm);
            message = parm[parm.Length - 1].Value.ToString();
            return parm[parm.Length - 2].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteModule(int id)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ModuleID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = id;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "DeleteModule", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        public ModuleInfo GetModuleByID(int id)
        {
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE ModuleID = {2}", Fields, Schema + ViewTableName, id);
            ModuleInfo info = new ModuleInfo();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            if (dr != null)
            {
                if (dr.Read())
                {
                    int i = 0;                    
                    info.ModuleID = dr.GetInt32(i++);
                    info.ModuleName = dr.GetString(i++);
                    info.ModuleLink = dr.GetString(i++);
                    info.ModuleMenu = dr.GetString(i++);
                    info.ModuleType = new ModuleTypeInfo();
                    info.ModuleType.ModuleTypeID = dr.GetInt32(i++);
                    info.ModuleType.ModuleTypeName = dr.GetString(i++);
                }
                dr.Close();
            }
            return info;
        }

        /// <summary>
        /// 模块列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public IList<ModuleInfo> ListModule(ModuleCondition condition, out int recordcount)
        {
            Wheres = new StringBuilder(" 1 = 1 ");
            if (condition.ModuleTypeID > 0)
                Wheres.AppendFormat(" AND ModuleTypeID = {0} ", condition.ModuleTypeID);
            if (!string.IsNullOrEmpty(condition.KeyWord))
                Wheres.AppendFormat(" AND ( ModuleName LIKE '%{0}%' OR ModuleLink LIKE '%{0}%' ) ", condition.KeyWord);
            recordcount = base.PageListCounter(Schema + ViewTableName, Wheres.ToString());
            SqlDataReader dr = base.PageListRecord(Fields
                , Schema + ViewTableName, Wheres.ToString(), condition.OrderBy, condition.PageIndex, condition.PageSize);
            List<ModuleInfo> list = new List<ModuleInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    int i = 0;
                    ModuleInfo info = new ModuleInfo();
                    info.ModuleID = dr.GetInt32(i++);
                    info.ModuleName = dr.GetString(i++);
                    info.ModuleLink = dr.GetString(i++);
                    info.ModuleMenu = dr.GetString(i++);
                    info.ModuleType = new ModuleTypeInfo();
                    info.ModuleType.ModuleTypeID = dr.GetInt32(i++);
                    info.ModuleType.ModuleTypeName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        #endregion
    }
}
